/*
 * @Description: 路由配置文件
 * @Version: 2.0
 * @Autor: lhl
 * @Date: 2021-04-05 14:07:15
 * @LastEditors: lhl
 * @LastEditTime: 2021-05-09 19:22:58
 */
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/home',
    component: () => import('@/components/layout/index.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          title: '首页',
          i18nTitle: 'tabBarItem.home',
          keepAlive: true
        },
        component: () => import('@/pages/home/index.vue')
      },
      {
        path: '/my',
        name: 'my',
        meta: {
          title: '我的',
          i18nTitle: 'tabBarItem.myCenter',
          keepAlive: true
        },
        component: () => import('@/pages/my/index.vue')
      },
      {
        path: '/product',
        name: 'product',
        meta: {
          title: '我的',
          i18nTitle: 'tabBarItem.product',
          keepAlive: true
        },
        component: () => import('@/pages/product/index.vue')
      },
      {
        path: '/vue3Study',
        name: 'vue3Study',
        meta: {
          title: 'vue3Study',
          i18nTitle: 'tabBarItem.vue3Study',
          keepAlive: true
        },
        component: () => import('@/pages/vue3Study/index.vue')
      }
    ]
  },
  {
    path: '/register',
    name: 'register',
    meta: {
      title: '注册'
    },
    component: () => import('@/pages/register/index.vue')
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
    component: () => import('@/pages/login/index.vue')
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

console.log(router, 'router')
// 路由前置钩子
router.beforeEach((to, from, next) => {
  console.log(from, 'from')
  const title = to.meta && (to.meta.title as string)
  if (title) {
    document.title = title
  }
  next()
})

router.afterEach((to, from) => {
  console.log(to, 'to', from, 'from')
})

// 路由配置上定义 路由独享的守卫
// beforeEnter: (to, from) => {
//   // reject the navigation
//   return false
// },

// 导航守卫
// onBeforeRouteLeave, onBeforeRouteUpdate

export default router
